﻿namespace ICMRemoteEdu
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;

    public class ICMEditorForm : Form
    {
        private IContainer components;
        private ICMCourse course = new ICMCourse("Новый курс");
        private MenuStrip menuStrip1;
        private ToolStripMenuItem mi_actions;
        private ToolStripMenuItem mi_clearcourse;
        private ToolStripMenuItem mi_cource;
        private ToolStripMenuItem mi_loadcourse;
        private ToolStripMenuItem mi_newcourse;
        private ToolStripMenuItem mi_savecourse;
        private PropertyGrid propertyEditor;
        public TreeView QuestionTree;
        private SplitContainer splitContainer1;

        public ICMEditorForm()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void ICMEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveCourse();
        }

        private void InitializeComponent()
        {
            this.splitContainer1 = new SplitContainer();
            this.QuestionTree = new TreeView();
            this.menuStrip1 = new MenuStrip();
            this.mi_cource = new ToolStripMenuItem();
            this.mi_newcourse = new ToolStripMenuItem();
            this.mi_loadcourse = new ToolStripMenuItem();
            this.mi_actions = new ToolStripMenuItem();
            this.mi_savecourse = new ToolStripMenuItem();
            this.mi_clearcourse = new ToolStripMenuItem();
            this.propertyEditor = new PropertyGrid();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            base.SuspendLayout();
            this.splitContainer1.Dock = DockStyle.Fill;
            this.splitContainer1.Location = new Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Panel1.Controls.Add(this.QuestionTree);
            this.splitContainer1.Panel1.Controls.Add(this.menuStrip1);
            this.splitContainer1.Panel2.Controls.Add(this.propertyEditor);
            this.splitContainer1.Size = new Size(0x279, 0x179);
            this.splitContainer1.SplitterDistance = 0xd3;
            this.splitContainer1.TabIndex = 0;
            this.QuestionTree.Dock = DockStyle.Fill;
            this.QuestionTree.Location = new Point(0, 0x18);
            this.QuestionTree.Name = "QuestionTree";
            this.QuestionTree.Size = new Size(0xd3, 0x161);
            this.QuestionTree.TabIndex = 0;
            this.menuStrip1.Items.AddRange(new ToolStripItem[] { this.mi_cource, this.mi_actions });
            this.menuStrip1.Location = new Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new Size(0xd3, 0x18);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            this.mi_cource.DropDownItems.AddRange(new ToolStripItem[] { this.mi_newcourse, this.mi_loadcourse });
            this.mi_cource.Name = "mi_cource";
            this.mi_cource.Size = new Size(0x2d, 20);
            this.mi_cource.Text = "Курс";
            this.mi_newcourse.Name = "mi_newcourse";
            this.mi_newcourse.Size = new Size(0x9c, 0x16);
            this.mi_newcourse.Text = "Новый курс";
            this.mi_newcourse.Click += new EventHandler(this.mi_newcourse_Click);
            this.mi_loadcourse.Name = "mi_loadcourse";
            this.mi_loadcourse.Size = new Size(0x9c, 0x16);
            this.mi_loadcourse.Text = "Загрузить курс";
            this.mi_loadcourse.Click += new EventHandler(this.mi_loadcourse_Click);
            this.mi_actions.DropDownItems.AddRange(new ToolStripItem[] { this.mi_savecourse, this.mi_clearcourse });
            this.mi_actions.Name = "mi_actions";
            this.mi_actions.Size = new Size(70, 20);
            this.mi_actions.Text = "Действия";
            this.mi_savecourse.Name = "mi_savecourse";
            this.mi_savecourse.Size = new Size(0x84, 0x16);
            this.mi_savecourse.Text = "Сохранить";
            this.mi_savecourse.Click += new EventHandler(this.mi_savecourse_Click);
            this.mi_clearcourse.Name = "mi_clearcourse";
            this.mi_clearcourse.Size = new Size(0x84, 0x16);
            this.mi_clearcourse.Text = "Очистить";
            this.mi_clearcourse.Click += new EventHandler(this.mi_clearcourse_Click);
            this.propertyEditor.Dock = DockStyle.Fill;
            this.propertyEditor.Location = new Point(0, 0);
            this.propertyEditor.Name = "propertyEditor";
            this.propertyEditor.Size = new Size(0x1a2, 0x179);
            this.propertyEditor.TabIndex = 0;
            this.propertyEditor.SelectedGridItemChanged += new SelectedGridItemChangedEventHandler(this.propertyEditor_SelectedGridItemChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x279, 0x179);
            base.Controls.Add(this.splitContainer1);
            base.MainMenuStrip = this.menuStrip1;
            base.Name = "ICMEditorForm";
            base.ShowInTaskbar = false;
            this.Text = "Редактор курсов";
            base.FormClosing += new FormClosingEventHandler(this.ICMEditor_FormClosing);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void mi_clearcourse_Click(object sender, EventArgs e)
        {
            this.course = new ICMCourse();
            ICMTreeManager.RefreshEditTree(this.QuestionTree, this.course);
        }

        private void mi_loadcourse_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.ShowDialog();
                using (FileStream stream = File.Open(dialog.FileName, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    this.course = (ICMCourse) formatter.Deserialize(stream);
                }
                ICMTreeManager.RefreshEditTree(this.QuestionTree, this.course);
                this.propertyEditor.SelectedObject = this.course;
            }
        }

        private void mi_newcourse_Click(object sender, EventArgs e)
        {
            this.propertyEditor.SelectedObject = this.course;
        }

        private void mi_savecourse_Click(object sender, EventArgs e)
        {
            this.SaveCourse();
        }

        private void propertyEditor_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
        {
            ICMTreeManager.RefreshEditTree(this.QuestionTree, this.course);
        }

        private void SaveCourse()
        {
            if (MessageBox.Show("Сохранить курс?", "Сохранение курса", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (SaveFileDialog dialog = new SaveFileDialog())
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        using (FileStream stream = File.Open(dialog.FileName, FileMode.Create, FileAccess.Write))
                        {
                            formatter.Serialize(stream, this.course);
                            stream.Flush();
                            stream.Close();
                        }
                    }
                }
                ICMTreeManager.RefreshEditTree(this.QuestionTree, this.course);
            }
        }
    }
}

